package utilities;

public class PasswordChecker {
	private boolean hasLowercase = false;
	private boolean hasUppercase = false;
	private boolean hasNumber = false;
	private boolean hasSpecialChar = false;
	private char[] lowCaseLet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
			 					 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
	private char[] upCaseLet = 	{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
			 					 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
	private char[] numbers = 	{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
	private char[] specChars = 	{'.', '-', '_', '+', '!', '?', '='};
	
	public boolean checkPassword(String password) {
		resetBooleans();
		// Set the boolean array that accepts or unaccepts all the chars as legal chars. Then set all the booleans to false.
		boolean[] charsAccepted = new boolean[password.length()];
		for(int i = 0; i < charsAccepted.length; i++) {
			charsAccepted[i] = false;
		}
		
		// Search for lowercase letter
		for(int i = 0; i < password.length(); i++) {
			if(charIsInLowercaseArray(password.charAt(i))) {
				hasLowercase = true;
				charsAccepted[i] = true;
			}
		}
				
		// Search for uppercase letter
		for(int i = 0; i < password.length(); i++) {
			if(charIsInUppercaseArray(password.charAt(i))) {
				hasUppercase = true;
				charsAccepted[i] = true;
			}
		}
		
		// Search for number
		for(int i = 0; i < password.length(); i++) {
			if(charIsInNumbersArray(password.charAt(i))) {
				hasNumber = true;
				charsAccepted[i] = true;
			}
		}
				
		// Search for special character
		for(int i = 0; i < password.length(); i++) {
			if(charIsInSpecialCharsArray(password.charAt(i))) {
				hasSpecialChar = true;
				charsAccepted[i] = true;
			}
		}
		
		boolean allCharsAccepted = true;
		for(int i = 0; i < charsAccepted.length; i++) {
			if(charsAccepted[i] == false) {allCharsAccepted = false;}
		}
		
		// Once all 4 types of chars have been found, and all chars have been accepted and if the password is longer than 6 chars, accept the password and return true
		if(hasLowercase == true && hasUppercase == true && hasNumber == true && hasSpecialChar == true && password.length() >= 6 && allCharsAccepted == true) {
			return true;	
		}
		else {
			return false;
		}
		
	}
	
	// Intuitively, resets all the "Has this kind of char" booleans to false
	private void resetBooleans() {
		hasLowercase = false;
		hasUppercase = false;
		hasNumber = false;
		hasSpecialChar = false;
	}
	
	// Checks if the given character "ch" exists in the lowercase array
	private boolean charIsInLowercaseArray(char ch) {
		boolean returner = false;
		for(int i = 0; i < lowCaseLet.length; i++) {
			if(ch == lowCaseLet[i]) {
				returner = true; 
				break;
			}
		}
		return returner;
	}
	
	//Checks if the given character "ch" exists in the uppercase array
	private boolean charIsInUppercaseArray(char ch) {
		boolean returner = false;
		for(int i = 0; i < upCaseLet.length; i++) {
			if(ch == upCaseLet[i]) {
				returner = true; 
				break;
			}
		}
		return returner;
	}
	
	//Checks if the given character "ch" exists in the numbers array
	private boolean charIsInNumbersArray(char ch) {
		boolean returner = false;
		for(int i = 0; i < numbers.length; i++) {
			if(ch == numbers[i]) {
				returner = true; 
				break;
			}
		}
		return returner;
	}
	
	//Checks if the given character "ch" exists in the special characters array
	private boolean charIsInSpecialCharsArray(char ch) {
		boolean returner = false;
		for(int i = 0; i < specChars.length; i++) {
			if(ch == specChars[i]) {
				returner = true; 
				break;
			}
		}
		return returner;
	}
}
